package com.xindz.cn.mvptest.components.product.view;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputEditText;
import android.support.v7.app.ActionBar;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.avos.avoscloud.AVException;
import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.xindz.cn.mvptest.R;
import com.xindz.cn.mvptest.base.BaseActivity;
import com.xindz.cn.mvptest.components.product.data.AttachProductBean;
import com.xindz.cn.mvptest.components.product.data.ProductBean;
import com.xindz.cn.mvptest.components.product.persenter.ProductGetPersenter;
import com.xindz.cn.mvptest.shared.core.glide.GlideApp;
import com.xindz.cn.mvptest.shared.core.image.ImageLoader;

import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @name MvpTest
 * @class name：com.xindz.cn.mvptest.components.product.view
 * @describe 获取一件商品
 * @anthor XINDZ
 * @time 2017/10/31 9:28
 * @change
 * @chang time
 * @class describe
 */
public class ProductGetActivity extends BaseActivity<ProductGetView, ProductGetPersenter> implements ProductGetView {
    @BindView(R.id.et_product_productid)
    TextInputEditText etProductProductid;
    @BindView(R.id.bt_product_get)
    Button btProductGet;
    @BindView(R.id.tv_product_show)
    TextView tvProductShow;
    @BindView(R.id.iv_product_get)
    ImageView ivProductGet;
    private String productID = "59fac918ee920a004437f5fb";
    private ImageLoader imageLoader;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_get);
        ButterKnife.bind(this);
        imageLoader = ImageLoader.getInstance(GlideApp.with(this));
        etProductProductid.setText(productID);
    }

    @Override
    public void getProduct(ProductBean productBean, AVException e) {
        if (e != null) ToastUtils.showShort("获取失败:" + e.getMessage());
        StringBuffer stringBuffer = new StringBuffer();
        Date createdAt = productBean.getDate("createdAt");
        stringBuffer.append("\n" + productBean.getTitle());
        stringBuffer.append("\n" + productBean.getDescription());
        stringBuffer.append("\n" + productBean.getXindz());
        stringBuffer.append("\n" + productBean.getPrice());
        stringBuffer.append("\n" + productBean.getDate("createdAt"));
        AttachProductBean attachProductBean = productBean.getAttachProductBean();
        String title = attachProductBean.getTitle();
        Date csdate = attachProductBean.getCedate();
        stringBuffer.append("\n" + title);
        stringBuffer.append("\n" + csdate);
        tvProductShow.setText(stringBuffer);
        imageLoader.defaultLoad(productBean.getImage()).into(ivProductGet);

    }

    @Override
    public ProductGetPersenter createPresneter() {
        return new ProductGetPersenter();
    }

    @Override
    public ProductGetView createView() {
        return this;
    }

    @OnClick(R.id.bt_product_get)
    public void onViewClicked() {
        String id = etProductProductid.getEditableText().toString();
        getPresneter().getProduct(id);
    }

}
